package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.config.Response;
import com.example.prison.controller.AlarmRecordController;
import com.example.prison.entity.AlarmRecord;
import com.example.prison.entity.PushManagement;
import com.example.prison.entity.PushRecord;
import com.example.prison.mapper.PushManagementMapper;
import com.example.prison.mapper.PushRecordMapper;
import com.example.prison.service.AlarmRecordService;
import com.example.prison.mapper.AlarmRecordMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
* @author jiang
* @description 针对表【alarm_record】的数据库操作Service实现
* @createDate 2023-05-29 17:22:08
*/
@Service
public class AlarmRecordServiceImpl extends ServiceImpl<AlarmRecordMapper, AlarmRecord>
    implements AlarmRecordService{

    @Autowired
    private AlarmRecordMapper alarmRecordMapper;

    @Autowired
    private PushRecordMapper pushRecordMapper;

    @Autowired
    private PushManagementMapper pushManagementMapper;

    @Override
    public PageInfo<AlarmRecord> get(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        if(ObjectUtils.isEmpty(alarmRecordMapper.get())) {
            return new PageInfo<>(alarmRecordMapper.get());
        }
        return new PageInfo<>(alarmRecordMapper.get());
    }

    @Override
    public int add(AlarmRecord record) {
        Integer res = 0;
        if(!ObjectUtils.isEmpty(alarmRecordMapper.getOneByNumberAndLevel(record.getNumber(), record.getLevel()))) {
            List<AlarmRecord> alarm = alarmRecordMapper.getOneByNumberAndLevel(record.getNumber(), record.getLevel());
            for(AlarmRecord a : alarm) {
                if(!a.getSolve().equals("已处理")) {
                    return -1;
                }
            }
        }
        alarmRecordMapper.add(record);
        if(ObjectUtils.isEmpty(pushManagementMapper.getOneByAlarmLevel(record.getLevel()))) {
            return 1;
        }
        PushManagement pushManage = pushManagementMapper.getOneByAlarmLevel(record.getLevel());
        PushRecord push = new PushRecord();
        push.setAlarmLevel(record.getLevel());
        if (ObjectUtils.isNotEmpty(pushManage.getPushPersonnel())) {
            push.setPushPersonnel(pushManage.getPushPersonnel());
        }
        if (ObjectUtils.isNotEmpty(pushManage.getNote())) {
            push.setNote(pushManage.getNote());
        }
        push.setPushTime(new SimpleDateFormat("yyyy/M/d HH:mm:ss").format(new Date()));
        pushRecordMapper.add(push);
        return 1;
    }

    @Override
    public void delete(Integer[] ids) {
        alarmRecordMapper.deleteRecord(ids);
    }

    @Override
    public int updateRecord(Integer id, String processPeople, String processTime) {
        alarmRecordMapper.updateRecord(id, processPeople, processTime);
        return 1;
    }

    @Override
    public int updatePushRecord(Integer id, String processPeople) {
        alarmRecordMapper.updatePushRecord(id, processPeople);
        return 1;
    }

    @Override
    public int handlePushAlarmRecord(Integer id, String processPeople) {
        AlarmRecord alarmDTO = alarmRecordMapper.getById(id);
        PushRecord recordDTO = new PushRecord();
        recordDTO.setAlarmLevel(alarmDTO.getLevel());
        recordDTO.setPushPersonnel(processPeople);
        if(ObjectUtils.isNotEmpty(pushManagementMapper.getOneByAlarmLevel(recordDTO.getAlarmLevel()).getNote())) {
            recordDTO.setNote(pushManagementMapper.getOneByAlarmLevel(recordDTO.getAlarmLevel()).getNote());
        }
        recordDTO.setPushTime(new SimpleDateFormat("yyyy/M/d HH:mm:ss").format(new Date()));
        pushRecordMapper.add(recordDTO);
        return 1;
    }

    @Override
    public AlarmRecord getById(Integer id) {
        return alarmRecordMapper.getById(id);
    }

    @Override
    public PageInfo<AlarmRecord> getByNumber(int pageNumber, int pageSize, String number) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(alarmRecordMapper.getByNumber(number));
    }

    @Override
    public PageInfo<AlarmRecord> getByTime(int pageNumber, int pageSize, String beginTime, String endTime) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(alarmRecordMapper.getByTime(beginTime, endTime));
    }

    @Override
    public PageInfo<AlarmRecord> getByLevel(int pageNumber, int pageSize, String level) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(alarmRecordMapper.getByLevel(level));
    }

    @Override
    public PageInfo<AlarmRecord> getByStatus(int pageNumber, int pageSize, String alarmStatus) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(alarmRecordMapper.getByStatus(alarmStatus));
    }

}




